package tcpFirmware.controller;

import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import tcpFirmware.*;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
import tcpFirmware.model.DBHelper;

public class LoginController {

	@Routes(route = "login")
	public void login(Socket socket, HashMap<String,String> head,HashMap<String,String> form) {
		String username= form.get("username");
		String password = form.get("password");
		
		Connection connection = new DBHelper().getConnection();
		try {
			Statement statement =connection.createStatement();
			ResultSet rs = statement.executeQuery("select* from Users where username='"+username+"'");
			rs.next();
			if(rs.getString("password").equals(password)) {
				ResponseBody responseBody = new ResponseBody(socket);
				responseBody.setHeader("登录状态", "登录成功").setType(Type.text.type).setHeader("身份", String.valueOf(rs.getInt("isAdmin")));
				responseBody.sendResponse();
			}else {
				ResponseBody responseBody = new ResponseBody(socket);
				responseBody.setHeader("登录状态", "登录失败").setType(Type.text.type);
				responseBody.sendResponse();
			}
			
		} catch (IOException|SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
